package com.fundamentalesandroid;

import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SoundPoolTest extends Activity implements OnTouchListener{

	SoundPool soundPool;
	int miSonidoId=-1;
	TextView textView;
	boolean state=false;
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
		if (event.getAction() == MotionEvent.ACTION_UP){
			if (!state){ 
				if(miSonidoId != -1){
					textView.setText("ReproducirSonido");
					soundPool.play(miSonidoId, 1, 1, 0, 0, 1);
					state=true;
				} else{
					textView.setText("No hay sonido");
				}
			}else{
				soundPool.pause(miSonidoId);
				state=false;
				textView.setText("Detener Sonido");
			}
		}
		return true;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textView=new TextView(this);
		textView.setOnTouchListener(this);
		setContentView(textView);
		//configurar controles de volumen
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		soundPool=new SoundPool(20,AudioManager.STREAM_MUSIC,0);
		try{
			AssetManager assetManager=getAssets();
			AssetFileDescriptor descriptor=assetManager.openFd("sound.ogg");
			miSonidoId=soundPool.load(descriptor, 1);
			textView.setText("Se leyo sonido");
		}catch(IOException e){
			textView.setText("No se ha podido cargar el efecto de sonido desde asset, "+
							e.getMessage());
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	
	
}
